package edu.hcmus.sow.dao;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.hibernate.exception.DataException;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import edu.hcmus.sow.domain.TaskAttachment;

/**
 * DAO to manage TaskAttachment entities.
 * 
 */
@Repository
@Transactional
public class TaskAttachmentDAOImpl extends AbstractHibernateDAO<TaskAttachment, Integer> implements TaskAttachmentDAO {

   public TaskAttachment findByName(String fileName) throws DataAccessException{
      List<TaskAttachment> list = findByCriteria(Restrictions.eq("fileName", fileName));
      return list.size() == 0 ? null : list.get(0);
   }
   
   public List<TaskAttachment> getAttachments(Integer taskID) throws DataException{
      String query = "from TaskAttachment ta " +
      		"join fetch ta.user tau " +
      		"join fetch ta.task tat " +
      		"where ta.task.id=?";
      List<TaskAttachment> list = executeQuery(query, taskID);
      return list;
   }
}
